package plugins.openform;

import kd.bos.base.AbstractBasePlugIn;
import kd.bos.base.BaseShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.Map;

/**
 * 基础资料插件
 */
public class OpenSchoolCard extends AbstractBasePlugIn implements Plugin {
    public void preOpenForm(PreOpenFormEventArgs e) {
        super.preOpenForm(e);

        // 1. 获取入口参数
        Map<String, Object> customParams = e.getFormShowParameter().getCustomParams();
        String identity = (String) customParams.get("Identity"); // 假设参数名为"param"

        // 2. 检查参数是否为"user"
        if ("user".equals(identity)) {
            // 获取当前用户名
            String currentUser = RequestContext.get().getUserName();
            // 查询匹配记录（假设基础资料字段标识为"name"）
            QFilter filter = new QFilter("dcb4_username", QCP.equals, currentUser);
            DynamicObject record = BusinessDataServiceHelper.loadSingle(
                    "dcb4_schoolcard", // 基础资料实体标识（如：bd_user）
                    "id",
                    new QFilter[]{filter}
            );
            if (record != null) {
                BaseShowParameter baseParam = (BaseShowParameter) e.getSource();
                baseParam.setPkId(record.getPkValue());
                baseParam.setStatus(OperationStatus.EDIT);
            }
        }
    }
}